package com.shroo8.footballblog.controllers.api;

import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shroo8.footballblog.bo.api.CreateArticleBo;
import com.shroo8.footballblog.dto.api.ArticleDto;


/**
 * Handles requests for api put new article.
 */
@Controller
public class CreateArticleController{
	
	private static final Logger logger = LoggerFactory.getLogger(CreateArticleController.class);

	@Resource(name="createArticleBo", type=com.shroo8.footballblog.bo.api.CreateArticleBoImpl.class)
	private CreateArticleBo createArticleBo;

	@RequestMapping(value = "/articles", method = RequestMethod.POST)
	public @ResponseBody String createArticle(Locale locale, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("Welcome to createArticle! The client locale is {}.", locale);
		
		String authorId = request.getParameter("authorId");
		String articleTitle = request.getParameter("articleTitle");
		String articleText = request.getParameter("articleContent");
		
		ArticleDto articleDto = new ArticleDto();
		articleDto.setAuthorId(authorId);
		articleDto.setArticleTitle(articleTitle);
		articleDto.setArticleText(articleText);
		
		ArticleDto createdArticleDto = createArticleBo.createArticle(articleDto);
		
		//ArticleDtoJsonWrapper createArticleResponse = new ArticleDtoJsonWrapper();
		//createArticleResponse.setReponseCode(200);
		//createArticleResponse.setResponseText("Ok");
		//createArticleResponse.setArticleDto(createdArticleDto);
		
		return "{\"articleId\":\"" + createdArticleDto.getArticleId() + "\"}";
	}

}
